package com.example.springboot.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.example.springboot.common.AuthAccess;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.service.UserService;

import cn.hutool.core.util.StrUtil;
import jakarta.annotation.Resource;



@RestController
public class WebController {

    @Resource
    UserService userService;

    @AuthAccess
    @GetMapping("/")
    public Result hello() {
        return Result.success("success");
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        if(StrUtil.isBlank(user.getCount()) || StrUtil.isBlank(user.getPwd())){
            return Result.error("数据输入不合法");
        }
        user=userService.login(user);
        return Result.success(user);
    }
    @AuthAccess
    @PostMapping("/registry")
    public Result registry(@RequestBody User user) {
        if(StrUtil.isBlank(user.getCount()) || StrUtil.isBlank(user.getPwd())){
            return Result.error("数据输入不合法");
        }
        user=userService.registry(user);
        return Result.success(user);
    }

    @AuthAccess
    @PutMapping("/password")
    public Result password(@RequestBody User user) {
        if(StrUtil.isBlank(user.getCount()) || StrUtil.isBlank(user.getPhone())){
            return Result.error("数据输入不合法");
        }
        userService.resetPassword(user);
        return Result.success();
    }
}
